﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Specialized;

namespace POSSystem.BLL
{
    public class View_Invoices : _View_Invoices
    {
        public static View_Invoices GetInvoiceByID(int id)
        {
            try
            {
                View_Invoices invoices = new View_Invoices();
                invoices.Where.ID.Value = id;
                invoices.Where.Deleted.Value = false;
                invoices.Query.Load();

                return invoices;
            }
            catch(Exception ex)
            {
                ErrorLog.WriteToFile("View_Invoices.cs", "GetInvoiceByID", ex.StackTrace);
                throw ex;
            }
        }

        public static View_Invoices GetInvoiceByNo(string no)
        {
            try
            {
                View_Invoices invoices = new View_Invoices();
                invoices.Where.InvoiceNo.Value = no;
                invoices.Where.Deleted.Value = false;
                invoices.Where.BranchID.Value = Branches.CurrentBranchID;
                invoices.Query.Load();

                return invoices;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_Invoices.cs", "GetInvoiceByID", ex.StackTrace);
                throw ex;
            }
        }

        public static View_Invoices GetInvoiceByCustID(int id)
        {
            try
            {
                View_Invoices invoices = new View_Invoices();
                invoices.Where.CustomerID.Value = id;
                invoices.Where.Deleted.Value = false;
                invoices.Where.BranchID.Value = Branches.CurrentBranchID;
                invoices.Query.Load();

                return invoices;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_Invoices.cs", "GetInvoiceByCustID", ex.StackTrace);
                throw ex;
            }
        }

        public static View_Invoices GetInvoiceByCustID(int id, string invoiceNo)
        {
            try
            {
                View_Invoices invoices = new View_Invoices();
                invoices.Where.CustomerID.Value = id;
                invoices.Where.Deleted.Value = false;
                if (invoiceNo != "")
                    invoices.Where.InvoiceNo.Value = invoiceNo;
                invoices.Where.BranchID.Value = Branches.CurrentBranchID;
                invoices.Query.Load();

                return invoices;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_Invoices.cs", "GetInvoiceByCustID", ex.StackTrace);
                throw ex;
            }
        }

        public static View_Invoices GetInvoices()
        {
            try
            {
                View_Invoices invoices = new View_Invoices();
                invoices.Where.BranchID.Value = Branches.CurrentBranchID;
                invoices.Query.Load();
                return invoices;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_Invoices.cs", "GetInvoices", ex.StackTrace);
                throw ex;
            }
        }

        public static View_Invoices GetUnpaidInvoices(int custID)
        {
            try
            {
                View_Invoices invoices = new View_Invoices();
                if (custID > 0)
                    invoices.Where.CustomerID.Value = custID;
                invoices.Where.Balance.Value = 0;
                invoices.Where.BranchID.Value = Branches.CurrentBranchID;
                invoices.Where.Deleted.Value = false;
                invoices.Where.Balance.Operator = MyGeneration.dOOdads.WhereParameter.Operand.NotEqual;
                invoices.Query.Load();

                return invoices;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_Invoices.cs", "GetUnpaidInvoices", ex.StackTrace);
                throw ex;
            }
        }

        public static View_Invoices GetInvoices(string invoiceNo)
        {
            try
            {
                View_Invoices invoices = new View_Invoices();
                if (invoiceNo != "")
                    invoices.Where.InvoiceNo.Value = invoiceNo;
                invoices.Where.BranchID.Value = Branches.CurrentBranchID;
                invoices.Where.Deleted.Value = false;
                invoices.Query.Load();

                return invoices;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_Invoices.cs", "GetInvoices", ex.StackTrace);
                throw ex;
            }
        }

        public static void GetAccountInfo(int custID, ref decimal charges, ref decimal paymentReceived)
        {
            try
            {
                View_Invoices invoices = new View_Invoices();

                ListDictionary parameters = new ListDictionary();

                parameters.Add(new SqlParameter("@custID", SqlDbType.Int, 0), custID);

                IDataReader reader = invoices.LoadFromSqlReader("GetAccountInfo", parameters);
                if (reader.Read())
                {
                    charges = (decimal)reader[View_Invoices.ColumnNames.TotalAmount];
                    paymentReceived = (decimal)reader[View_Invoices.ColumnNames.PaidAmount];
                }
                else
                {
                    charges = 0;
                    paymentReceived = 0;
                }
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_Invoices.cs", "GetInvoices", ex.StackTrace);
                throw ex;
            }
        }
    }
}
